package com.ys.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.sys.SysMapper;
import com.ys.util.TokenUtil;

/**
 * 判断登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private DiySqlMapper diySqlMapper;
    @Autowired
    private SysMapper sysMapper;

    /**
     * 开始进入地址请求拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

//       Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (int i = 0; i < cookies.length; i++) {
//                if (cookies[i].getName().equals(TokenUtil.ADMIN_TOKEN)) {
//                    String value = cookies[i].getValue();
//                    if (value != null && value.length() > 0) {
//                        Long loginId = TokenUtil.getAdminId(request);
//                        String token = sysMapper.getAdminToken(loginId);
//                        if (token != null) {
//                            if (value.indexOf(token) > -1) {
//                                return true;
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        response.sendRedirect("/web/admin/login");    //未登录，跳转到登录页
//        return false;
    	return true;
    }

    /**
     * 处理请求完成后视图渲染之前的处理操作
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    /**
     * 视图渲染之后的操作
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

}
